package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.RecordBean;
import com.project.bean.RoomBean;
import com.project.mapper.IRecordMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RecordServiceImp implements IRecordService{

    @Autowired
    private IRecordMapper recordMapper;
    @Autowired
    private IRoomMapper roomMapper;


    @Override
    public List<RecordBean> findByRoomID(Integer roomID) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("fk_roomid",roomID);
        return recordMapper.selectList(qw);
    }

    @Override
    public void add(RecordBean recordBean) {
        recordMapper.insert(recordBean);
        RoomBean roomBean = roomMapper.selectById(recordBean.getRecordRoomID());
        roomBean.setRoomStatus("设施损坏");
        roomMapper.updateById(roomBean);
    }

    @Override
    public void update(Integer recordID, Integer roomID) {
        RecordBean recordBean=recordMapper.selectById(recordID);
        recordBean.setRecordStatus("已解决");
        recordMapper.updateById(recordBean);

        QueryWrapper<RecordBean> qw=new QueryWrapper<>();
        qw.eq("re_recordstatus","未解决");
        qw.eq("fk_roomid",roomID);
        List list = recordMapper.selectList(qw);
        if (list.size()==0) {
            RoomBean roomBean = roomMapper.selectById(recordID);
            roomBean.setRoomStatus("正常");
            roomMapper.updateById(roomBean);
        }
    }
}
